#include "stdafx.h"
#include "UCButton.h"
#include "cuifunc.h"
#include "UEvent.h"


CUCButton::CUCButton(void)
{
	lstrcpy(m_szClsName,_T("UCButton"));
}
CUCButton::~CUCButton(void)
{

}

LPCTSTR CUCButton::GetClass() const
{
	return m_szClsName;
}

int CUCButton::SetLeftNormalImage(LPCTSTR lpszImage)
{
	m_sLeftNormalImage = lpszImage;
	return 0;
}

int CUCButton::SetRightNormalImage(LPCTSTR lpszImage)
{
	m_sRightNormalImage = lpszImage;
	return 0;
}




int CUCButton::SetCenterNormalImage(LPCTSTR lpszImage)
{
	m_sCenterNormalImage = lpszImage;
	return 0;
}

void CUCButton::DrawText(HDC hDC,const RECT& rcPaint)
{
	if(!m_sText.IsEmpty() )
	{
		int nLinks = 0;
		RECT rc = m_rcItem;
		rc.left += m_rcTextPadding.left;
		rc.right -= m_rcTextPadding.right;
		rc.top += m_rcTextPadding.top;
		rc.bottom -= m_rcTextPadding.bottom;

		DWORD clrColor = IsEnabled()?m_dwTextColor:m_dwDisabledTextColor;

		if (m_uButtonState & UISTATE_PUSHED)
		{
			::OffsetRect(&rc,1,1);
			clrColor = GetPushedTextColor();
		}
		else if(m_uButtonState & UISTATE_HOT)
		{
			clrColor = GetHotTextColor();
		}
		else if(m_uButtonState & UISTATE_FOCUSED)
		{
			clrColor = GetFocusedTextColor();
		}


		if( m_bShowHtml )
		{
			CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, clrColor, NULL, NULL, nLinks, m_uTextStyle);
		}
		else
		{
			CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, clrColor, m_iFont, m_uTextStyle);
		}
	}
}

void CUCButton::DrawImage(HDC hDC,const RECT& rcPaint)
{
	RECT rcCenter = m_rcItem;

	//draw left image
	if (!m_sLeftNormalImage.IsEmpty())
	{
		const TImageInfo *info = CUIC::GetImageInfo(m_pManager,(LPCTSTR)m_sLeftNormalImage);
		if (info)
		{
			RECT rc = m_rcItem;
			rc.right = rc.left + info->nX;
			rcCenter.left = rc.right;
			CRenderEngine::DrawImageString(hDC, m_pManager, rc, m_rcPaint,(LPCTSTR)m_sLeftNormalImage , NULL);
		}
	}


	//draw right image
	if (!m_sRightNormalImage.IsEmpty())
	{
		const TImageInfo *info = CUIC::GetImageInfo(m_pManager,(LPCTSTR)m_sRightNormalImage);
		if (info)
		{
			RECT rc = m_rcItem;
			rc.left = rc.right - info->nX;
			rcCenter.right  = rc.left;
			CRenderEngine::DrawImageString(hDC, m_pManager, rc, m_rcPaint,(LPCTSTR)m_sRightNormalImage , NULL);
		}
	}


	//draw center image
	if (!m_sCenterNormalImage.IsEmpty())
	{
		CRenderEngine::DrawImageString(hDC, m_pManager, rcCenter, m_rcPaint,(LPCTSTR)m_sCenterNormalImage , NULL);
	}
}

void CUCButton::DoPaint(HDC hDC, const RECT& rcPaint)
{
	if( !::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem) ) 
	{
		return;
	}

	PaintBkColor(hDC);
	DrawImage(hDC,rcPaint);
	DrawText(hDC,rcPaint);
	PaintBorder(hDC);
}



void CUCButton::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if (_tcsicmp(pstrName, _T("LeftNormalImage")) == 0)
	{
		SetLeftNormalImage(pstrValue);
	}
	else if (_tcsicmp(pstrName, _T("CenterNormalImage")) == 0)
	{
		SetCenterNormalImage(pstrValue);
	}
	else if (_tcsicmp(pstrName, _T("RightNormalImage")) == 0)
	{
		SetRightNormalImage(pstrValue);
	}
	else
	{
		CButtonUI::SetAttribute(pstrName,pstrValue);
	}
}


